package org.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.demo.entity.CultivateLog;
import org.example.demo.mapper.CultivateLogMapper;
import org.example.demo.service.CultivateLogService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * 培训记录服务实现类
 */
@Service
public class CultivateLogServiceImpl extends ServiceImpl<CultivateLogMapper, CultivateLog>
        implements CultivateLogService {

    @Override
    public Page<CultivateLog> selectPage(CultivateLog cultivateLog, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<CultivateLog> queryWrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        if (cultivateLog != null) {
            if (StringUtils.hasText(cultivateLog.getCultivateName())) {
                queryWrapper.like(CultivateLog::getCultivateName, cultivateLog.getCultivateName());
            }
            if (StringUtils.hasText(cultivateLog.getCultivateContent())) {
                queryWrapper.like(CultivateLog::getCultivateContent, cultivateLog.getCultivateContent());
            }
            if (StringUtils.hasText(cultivateLog.getCultivateTeacher())) {
                queryWrapper.like(CultivateLog::getCultivateTeacher, cultivateLog.getCultivateTeacher());
            }
            if (StringUtils.hasText(cultivateLog.getCultivatePlace())) {
                queryWrapper.like(CultivateLog::getCultivatePlace, cultivateLog.getCultivatePlace());
            }
            if (cultivateLog.getCultivateType() != null) {
                queryWrapper.eq(CultivateLog::getCultivateType, cultivateLog.getCultivateType());
            }
            if (cultivateLog.getCultivateStatus() != null) {
                queryWrapper.eq(CultivateLog::getCultivateStatus, cultivateLog.getCultivateStatus());
            }
        }

        // 按开始时间降序排序
        queryWrapper.orderByDesc(CultivateLog::getStartTime);

        return page(new Page<>(pageNum, pageSize), queryWrapper);
    }

    @Override
    public Page<CultivateLog> getByCultivateType(Integer cultivateType, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<CultivateLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CultivateLog::getCultivateType, cultivateType)
                .orderByDesc(CultivateLog::getStartTime);

        return page(new Page<>(pageNum, pageSize), queryWrapper);
    }

    @Override
    public Page<CultivateLog> getByCultivateStatus(Integer cultivateStatus, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<CultivateLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CultivateLog::getCultivateStatus, cultivateStatus)
                .orderByDesc(CultivateLog::getStartTime);

        return page(new Page<>(pageNum, pageSize), queryWrapper);
    }

    @Override
    public boolean save(CultivateLog entity) {
        LocalDateTime now = LocalDateTime.now();
        entity.setCreateTime(now);
        entity.setUpdateTime(now);
        return super.save(entity);
    }

    @Override
    public boolean updateById(CultivateLog entity) {
        entity.setUpdateTime(LocalDateTime.now());
        return super.updateById(entity);
    }
}